package m.html.list;

import m.html.element.Element;
import m.html.element.Text;
import m.html.element.other.Param;
import m.system.util.GenerateID;
import m.system.util.StringUtil;

import java.util.*;

public class ListEvent {

	private String event;
	private final String url;
	private final EventType type;
	private SuccessMethod success;
	private final int openWidth;
	private final List<Param> params;
	private final List<Param> queryParams;
	private final Set<String> paramFields;
	

	public ListEvent(String url) {
		this("click",url,EventType.AJAX,600);
	}
	public ListEvent(String event,String url) {
		this(event,url,EventType.AJAX,600);
	}
	public ListEvent(String url,EventType type) {
		this("click",url,type,600);
	}
	public ListEvent(String event,String url,EventType type) {
		this(event,url,type,600);
	}
	public ListEvent(String url,EventType type,int openWidth) {
		this("click",url,type,openWidth,SuccessMethod.NONE);
	}
	public ListEvent(String event,String url,EventType type,int openWidth) {
		this(event,url,type,openWidth,SuccessMethod.NONE);
	}
	public ListEvent(String url,EventType type,int openWidth,SuccessMethod success) {
		this("click",url,type,openWidth,success);
	}
	/**
	 * 
	 *  event 事件 click
	 *  url 
	 *  type
	 *  openWidth
	 *  success
	 */
	public ListEvent(String event,String url,EventType type,int openWidth,SuccessMethod success) {
		this.event=event;
		this.url=url;
		this.type=type;
		this.openWidth=openWidth;
		this.params=new ArrayList<>();
		this.queryParams=new ArrayList<>();
		this.paramFields=new HashSet<>();
		this.success=success;
	}
	/**
	 * 设置成功后方法
	 *  success
	 * 
	 */
	public ListEvent setSuccess(SuccessMethod success) {
		this.success = success;
		return this;
	}
	/**
	 * 更改触发事件
	 *  event
	 * 
	 */
	public ListEvent setEvent(String event) {
		this.event=event;
		return this;
	}
	/**
	 * 添加参数
	 *  p
	 * 
	 */
	public ListEvent addParam(Param p) {
		return this.addParam(null,p);
	}
	public ListEvent addParam(String name,String field) {
		return this.addParam(Param.field(name,field));
	}

	/**
	 * 添加参数  构造VueCollectResult时才能使用
	 */
	public ListEvent addParam(String a,Param p) {
		this.params.add(p);
		if(!StringUtil.isSpace(p.getField())) {
			if(StringUtil.isSpace(a)){
				this.paramFields.add(p.getField());
			}else{
				this.paramFields.add(p.getField()+" "+a);
				p.setField(a);
			}
		}
		return this;
	}
	/**
	 * 获取参数所需的字段
	 * 
	 */
	public Set<String> getParamFields() {
		return this.paramFields;
		
	}
	/**
	 * 添加查询参数
	 *  p
	 * 
	 */
	public ListEvent addQueryParam(Param p) {
		this.queryParams.add(p);
		return this;
	}
	public ListEvent addQueryParams(Param... ps){
		this.queryParams.addAll(Arrays.asList(ps));
		return this;
	}
	private String getString(List<Param> ps) {
		StringBuilder sb=new StringBuilder();
		for(Param p : ps) {
			sb.append(",{\"name\":\"").append(p.getName()).append("\",");
			if(!StringUtil.isSpace(p.getField()))
				sb.append("\"field\":\"").append(p.getField()).append("\",");
			if(!StringUtil.isSpace(p.getValue()))
				sb.append("\"value\":\"").append(p.getValue()).append("\",");
			sb.append("}");
		}
		if(sb.length()>0) {
			return sb.substring(1);
		}
		return sb.toString();
	}
	public String toExtendString(){
		return toExtendString("");
	}
	private String toExtendString(String cid) {
		StringBuilder sb=new StringBuilder();
		if(this.type==EventType.BACK){
			sb.append(" @").append(this.event).append("='listEvent(record,{")
				.append("\"type\":\"").append(this.type).append("\",")
				.append("\"params\":[").append(getString(this.params)).append("],")
				.append("\"queryParams\":[").append(getString(this.queryParams)).append("]")
				.append("},index,$event);' ");
		}else {
			sb.append(" @").append(this.event).append("='listEvent(record,{")
				.append("\"url\":\"").append(this.url).append("\",")
				.append("\"type\":\"").append(this.type).append(cid).append("\",")
				.append("\"openWidth\":\"").append(this.openWidth).append("px\",")
				.append("\"params\":[").append(getString(this.params)).append("],")
				.append("\"queryParams\":[").append(getString(this.queryParams)).append("],")
				.append("\"success\":\"").append(this.success).append("\"")
				.append("},index,$event);' ");
		}
		return sb.toString();
	}
	public Element toEventElement(Element target,String confirm){
		if(this.type==EventType.POPOVER){
			String cid=GenerateID.tempKey();
			String trigger= this.event.equalsIgnoreCase("click")?"click":"hover";
			String ext=toExtendString(cid).replaceAll("@"+this.event+"=","@openChange=");
			Element pop=Element.tag("a-popover").setExtend(ext).A("v-model:open","popoverShow['POPOVER"+cid+"'+index]")
					.A("trigger",trigger).A("overlayClassName","index-popover");
			pop.E(new Text("<template #content><div :id=\"'POPOVER"+cid+"'+index+'_'+key\" :style=\"{width:popoverWidth['POPOVER"+cid+"'+index]}\"></div></template>"));
			pop.E(target);
			return pop;
		}else{
			if(StringUtil.isSpace(confirm)) {
				target.setExtend(toExtendString());
				return target;
			}else {
				Element le=Element.tag("a-popconfirm").A("placement", "left").A("title",confirm);
				le.E(target);
				le.setExtend(toExtendString().replaceAll("@"+this.event+"=","@confirm="));
				return le;
			}
		}
	}
}
